from ..extensions import db
from datetime import datetime

class Payment(db.Model):
    __tablename__ = 'payments'
    
    id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), primary_key=True, autoincrement=True, comment='缴费ID')
    user_id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), nullable=False, comment='缴费用户，逻辑外键 users.id')
    house_id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), comment='关联房屋，逻辑外键 houses.id')
    parking_id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), comment='关联车位，逻辑外键 parking_spaces.id')
    name = db.Column(db.String(200), nullable=False, comment='费用名称')
    description = db.Column(db.Text, comment='费用描述')
    amount = db.Column(db.Numeric(10, 2), nullable=False, comment='费用大小')
    is_paid = db.Column(db.Boolean, nullable=False, default=False, comment='是否缴费，0=未缴，1=已缴')
    created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow, comment='创建时间')
    
    def to_dict(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'house_id': self.house_id,
            'parking_id': self.parking_id,
            'name': self.name,
            'description': self.description,
            'amount': float(self.amount),  # 转换为浮点数便于JSON序列化
            'is_paid': bool(self.is_paid),
            'created_at': str(self.created_at)
        }